<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card>
        <el-tabs>
          <!-- 放置页签 -->
          <el-tab-pane label="角色管理">
            <!-- 新增角色按钮 -->
            <el-row style="height: 60px">
              <el-button
                icon="el-icon-plus"
                size="small"
                type="primary"
                @click="isShowDialog=true"
              >新增角色</el-button>
            </el-row>
            <!-- 表格 -->
            <el-table :data="roleList" border="">
              <el-table-column prop="name" label="角色名称" width="240" />
              <el-table-column prop="description" label="描述" />
              <el-table-column label="操作">
                <template v-slot="scope">
                  <el-button size="small" type="success" @click="showPerm(scope.row.id)">分配权限</el-button>
                  <el-button size="small" type="primary" @click="showEdit(scope.row.id)">编辑</el-button>
                  <el-button size="small" type="danger" @click="delRole(scope.row.id)">删除</el-button>
                </template>
              </el-table-column>
            </el-table>
            <!-- 分页组件 -->
            <el-row
              type="flex"
              justify="center"
              align="middle"
              style="height: 60px"
            >
              <!-- 分页组件 -->
              <el-pagination
                layout="prev,pager,next"
                :page-size="pageSetting.pagesize"
                :total="total"
                @current-change="currentChange"
              />
            </el-row>
          </el-tab-pane>
          <el-tab-pane label="公司信息">
            <el-form label-width="120px" style="margin-top: 50px">
              <el-form-item label="公司名称">
                <el-input v-model="companyInfo.name" disabled style="width: 400px" />
              </el-form-item>
              <el-form-item label="公司地址">
                <el-input v-model="companyInfo.companyAddress" disabled style="width: 400px" />
              </el-form-item>
              <el-form-item label="邮箱">
                <el-input v-model="companyInfo.mailbox" disabled style="width: 400px" />
              </el-form-item>
              <el-form-item label="备注">
                <el-input
                  v-model="companyInfo.remarks"
                  type="textarea"
                  :rows="3"
                  disabled
                  style="width: 400px"
                />
              </el-form-item>
            </el-form>
          </el-tab-pane>
        </el-tabs>
      </el-card>
      <!-- 弹窗占位 -->
      <el-dialog :title="formData.id?'编辑角色':'新增角色'" :visible="isShowDialog" @close="btnCancel">
        <el-form ref="addForm" :model="formData" :rules="rules">
          <el-form-item label="角色名称" prop="name"><el-input v-model="formData.name" /></el-form-item>
          <el-form-item label="角色描述" prop="description"><el-input v-model="formData.description" /></el-form-item>

        </el-form>
        <template #footer>
          <el-button type="primary" @click="btnOk">确定</el-button>
          <el-button @click="btnCancel">取消</el-button>
        </template>
      </el-dialog>
      <!-- 分配权限弹窗 -->
      <el-dialog title="分配权限" :visible="isShowPerm" @close="btnCancelPerm">
        <el-checkbox-group v-model="checkList">
          <el-tree :data="permList" default-expand-all>
            <template v-slot="scope">
              <el-checkbox
                :key="scope.data.id"
                :label="scope.data.id"
              >
                {{ scope.data.name }}
              </el-checkbox>
            </template>
          </el-tree>
        </el-checkbox-group>

        <template #footer>
          <el-button type="primary" @click="btnOkPerm">确定</el-button>
          <el-button @click="btnCancelPerm">取消</el-button>
        </template>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import { getCompanyInfo, getRoleList, addRole, getRoleDetail, editRole, delRole, assignPerm } from '@/api/setting.js'
import { getPermissionList } from '@/api/permission'
import { listToTreeData } from '@/utils/index'
export default {
  data() {
    return {
      roleId: '',
      isShowPerm: false,
      checkList: [],
      permList: [],
      companyInfo: {},
      roleList: [],
      pageSetting: {
        page: 1,
        pagesize: 2
      },
      total: 0,
      isShowDialog: false,
      formData: {
        name: '',
        description: ''
      },
      rules: {
        name: [
          { required: true, message: '角色不能为空', trigger: 'blur' },
          { min: 2, max: 6, message: '输入字符在2-6之间', trigger: 'blur' }
        ],
        description: [
          { required: true, message: '角色不能为空', trigger: 'blur' },
          { min: 5, max: 10, message: '输入字符在5-10之间', trigger: 'blur' }

        ]
      }
    }
  },
  async created() {
    // 获取公司信息
    const res = await getCompanyInfo(this.$store.state.user.userInfo.id)
    console.log(res.data)
    this.companyInfo = res.data
    // 获取角色信息
    this.loadPage()
    // 获取权限信息总列表
    const res1 = await getPermissionList()
    console.log('这是权限列表', res1)
    this.permList = listToTreeData(res1.data, '0')
  },
  methods: {
    currentChange(newPage) {
      this.pageSetting.page = newPage
      this.loadPage()
    },
    // 获取信息加载页面
    async loadPage() {
      const res1 = await getRoleList(this.pageSetting)
      console.log(res1.data)
      this.total = res1.data.total
      this.roleList = res1.data.rows
    },
    async btnOk() {
      try {
        this.$refs.addForm.validate()
        if (this.formData.id) {
          await editRole(this.formData)
          this.$message.success('修改成功')
        } else {
          await addRole(this.formData)
          this.$message.success('添加成功')
        }

        this.loadPage()
        this.formData = {
          name: '',
          description: ''
        }
        this.isShowDialog = false
      } catch (error) {
        console.log(error)
      }
    },
    btnCancel() {
      this.isShowDialog = false
      this.formData = {
        name: '',
        description: ''
      }
      this.$refs.addForm.resetFields()
    },
    async showEdit(id) {
      const res = await getRoleDetail(id)
      this.formData = res.data
      this.isShowDialog = true
      console.log(this.formData)
    },
    async delRole(id) {
      await this.$confirm('是否确认删除？')
      await delRole(id)
      this.loadPage()
      this.$message.success('删除成功')
    },
    async btnOkPerm() {
      await assignPerm({
        id: this.roleId,
        permIds: this.checkList
      })
      this.$message.success('修改权限成功')

      this.isShowPerm = false
    },
    btnCancelPerm() {
      this.checkList = []
      this.isShowPerm = false
    },
    async showPerm(id) {
      const res = await getRoleDetail(id)
      console.log('员工原有的权限', res)
      this.roleId = id
      this.checkList = res.data.permIds
      this.isShowPerm = true
    }
  }

}
</script>

<style>

</style>
